﻿using System;
using System.Collections.Generic;
using System.Transactions;
using Common;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class UserPhotoBusiness
    {
        private const string Waming = "Business.UserPhotoBusiness";
        private readonly UserAlbumDAL userAlbumDal = new UserAlbumDAL();
        private readonly UserPhotoDAL userPhotoDal = new UserPhotoDAL();
        private readonly AnnexDAL annexDal = new AnnexDAL();

        public bool Save(UserAlbumDto dto)
        {
            try
            {
                return userAlbumDal.Save(dto);
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(UserAlbumDto dto)", ex);
                return false;
            }
        }

        public List<UserAlbumDto> GetUserAlbumDtos(int userId)
        {
            try
            {
                List<UserAlbumDto> dtos = null;
                var hts = userAlbumDal.FindAll(userId);
                if (hts != null)
                {
                    dtos = new List<UserAlbumDto>();
                    foreach (var ht in hts)
                    {
                        dtos.Add(FillDto.FillUserAlbumDto(ht));
                    }
                }
                return dtos;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetUserAlbumDtos(int userId)", ex);
                return null;
            }
        }

        public List<UserPhotoDto> GetPhotoByAlbumId(int albumId)
        {
            try
            {
                List<UserPhotoDto> photoDtos = null;
                var hts = userPhotoDal.FindAll(0, albumId, "");
                if (hts != null && hts.Count > 0)
                {
                    photoDtos = new List<UserPhotoDto>();
                    foreach (var ht in hts)
                    {
                        photoDtos.Add(FillDto.FillUserPhotoDto(ht));
                    }
                }
                return photoDtos;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetPhotoByAlbumId(int albumId)", ex);
                return null;
            }
        }

        public UserAlbumDto GetUserAlbumById(int id)
        {
            try
            {
                UserAlbumDto dto = null;
                var ht = userAlbumDal.FindById(id);
                if (ht != null)
                {
                    dto = FillDto.FillUserAlbumDto(ht);
                }
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetUserAlbumById(int id)", ex);
                return null;
            }
        }

        public string GetPhotoJSON(int userId)
        {
            var json = "";
            var albumDtos = GetUserAlbumDtos(userId);
            foreach (var albumDto in albumDtos)
            {
                var photoDtos = GetPhotoByAlbumId(albumDto.AlbumId) ?? new List<UserPhotoDto>();
                var photoJson = "";
                foreach (var photoDto in photoDtos)
                {
                    var annexBusiness = new AnnexBusiness();
                    var annexDto = annexBusiness.GetAnnexById(photoDto.PhotoAnnexId);
                    photoJson += photoJson != "" ? "," : "";
                    photoJson += "{\"PhotoName\":\"" + photoDto.PhotoName + "\",\"AnnexID\":\"" + photoDto.PhotoAnnexId +
                                 "\",\"ImageWidth\":\"" + annexDto.AnnexWidth + "\",\"ImageHeight\":\"" +
                                 annexDto.AnnexHeight + "\",\"PhotoId\":\"" + photoDto.PhotoId + "\"}";
                }
                json += json != "" ? "," : "";
                json += "{\"AlbumId\":\"" + albumDto.AlbumId + "\",\"AlbumName\":\"" +
                        albumDto.AlbumName + "\",\"PhotoList\":[" + photoJson + "]}";
            }
            return json;
        }

        public bool SaveUserAlbum(UserPhotoDto photoDto, AnnexDto annexDto, AnnexDto oldAnnexDto, bool isCover, out int returnAnnexId)
        {
            returnAnnexId = 0;
            var b = true;
            var tran = new TransactionScope();
            try
            {
                int annexId;
                if (annexDto != null)
                {
                    annexId = annexDal.Save(annexDto);
                    if (oldAnnexDto != null)
                    {
                        annexDal.Delete(oldAnnexDto.AnnexID);
                    }
                }
                else
                {
                    annexId = oldAnnexDto.AnnexID;
                }
                if (annexId > 0)
                {
                    photoDto.PhotoAnnexId = annexId;
                    b = userPhotoDal.Save(photoDto);
                    if (b)
                    {
                        if (isCover)
                        {
                            var albumDto = GetUserAlbumById(photoDto.PhotoAlbumId);
                            //这里设置相册封面，还未完成
                            b = userAlbumDal.Save(albumDto);
                        }
                    }
                    returnAnnexId = annexId;
                }
                if (b)
                {
                    tran.Complete();
                }
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError("Liuti.Business.AlbumBusiness.SaveUserAlbum(UserAlbumDto userAlbumDto, AnnexDto annexDto, bool isCover)", ex);
                return false;
            }
            finally
            {
                tran.Dispose();
            }
            return b;
        }
    }
}
